<?php

// This will be accesed only though AJAX

switch( $URL[1] )
{
	case 'send':
		$text = filter_cross( $_POST['text'] );
		$user = $User->get_id();
		$last_chat_id = $_POST['last_chat_id'];
		$u_row = $User->get_database_row();
		$username = $u_row[ $User->TABLE_COLUMNS['username'] ];
		
		$post_result = $Chat->post( $text, $user, $username );
		if ( $post_result )
		{
			$new_chat = $Chat->get( $last_chat_id );
			$_ajax_message = 'OK';
			if ( count( $new_chat['lines'] ) )
			{
				$_ajax_data['chat_lines'] = $new_chat;
			}
		}
		break;
		
	case 'get':
		$_ajax_message = 'OK';
		$last_chat_id = (int)$_POST['last_chat_id'];
		
		$time_start = time();
		$comet = false;
		do
		{
			$new_chat = $Chat->get( $last_chat_id );
			$t = time();
			
			if ( ( $time_start + 5 ) < $t )
			{
				//my_DBG( $User->get_id() .' time out BREAK', 1 );
				$comet = false;
			}
			if ( count( $new_chat['lines'] ) > 0 )
			{
				//my_DBG( $User->get_id() .' new lines BREAK', 1 );				
				$comet = false;
			}
			
			usleep( 100 );
		}
		while( $comet );
		$_ajax_data['chat_lines'] = $new_chat;	
		break;
}

?>